package timberman.character;

import timberman.core.MyGame;
import timberman.core.ScreenUtils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class Man extends Actor {
	static enum MAN_POSITION {
		LEFT, RIGHT
	};

	ManLibrary manLibrary;
	private Animation animation;
	private TextureRegion reg;
	private float stateTime;
	boolean isChop = false;

	public Man() {
		manLibrary = new ManLibrary();
		this.animation = manLibrary.getMan("BASICMAN");
		setSize(100, 100);
		reg = animation.getKeyFrame(0, true);

	}

	public void changeManType(String manTypeName) {
		this.animation = manLibrary.getMan(manTypeName);
		animation.setPlayMode(Animation.PlayMode.NORMAL);
		reg = animation.getKeyFrame(0, true);
	}

	public void changePosition(int x_pos) {
		MAN_POSITION type;
		float width = Gdx.graphics.getWidth();
		System.out.println(x_pos + "----assumeWidth = " + width / 2);
		if (x_pos < width / 2) {
			type = MAN_POSITION.LEFT;
			System.out.println("Da vao LEFT");
		} else {
			System.out.println("Da vao Right");
			type = MAN_POSITION.RIGHT;
		}
		switch (type) {
		case LEFT:
			float x_left = width / 4
					- animation.getKeyFrame(0).getRegionWidth() / 2;
			setPosition(200, 200);
			System.out.println("LEFT : (" + getX() + "," + getY() + ")");
			break;
		case RIGHT:
			float x_right = width * 3 / 4
					- animation.getKeyFrame(0).getRegionWidth() / 2;
			setPosition(400, 200);
			System.out.println("Right : (" + getX() + "," + getY() + ")");
			break;
		default:
			break;
		}
	}

	@Override
	public void act(float delta) {
		if (isChop) {
			actChop(delta);
		}
	}

	public void actChop(float delta) {
		stateTime += delta;
		reg = animation.getKeyFrame(stateTime, true);
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		batch.draw(reg, getX(), getY());
	}

	public Animation getAnimation() {
		return animation;
	}

	public void setChop(boolean isChop) {
		this.isChop = isChop;
	}

}
